package com.gnerv.battle.boot.framework.core.tools;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * DateTime 工具类
 * </p>
 *
 * @author Gnerv LiGen
 */
public class DateTimeTools {

    public static final DateTimeFormatter YYYY = DateTimeFormatter.ofPattern("yyyy");
    public static final DateTimeFormatter HYPHEN_YYYY_MM = DateTimeFormatter.ofPattern("yyyy-MM");
    public static final DateTimeFormatter HYPHEN_YYYY_MM_DD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter HYPHEN_SPACE_YYYY_MM_DD_HH = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
    public static final DateTimeFormatter HYPHEN_SPACE_COLON_YYYY_MM_DD_HH_MM = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    public static final DateTimeFormatter HYPHEN_SPACE_COLON_YYYY_MM_DD_HH_MM_SS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter HYPHEN_SPACE_COLON_SPACE_YYYY_MM_DD_HH_MM_SS_SSS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");

    public static final DateTimeFormatter UNDERSCORE_YYYY_MM = DateTimeFormatter.ofPattern("yyyy_MM");
    public static final DateTimeFormatter SLASH_YYYY_MM = DateTimeFormatter.ofPattern("yyyy/MM");

    public static final DateTimeFormatter HYPHEN_T_COLON_YYYY_MM_DD_HH_MM_SS = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");


    private DateTimeTools() {
    }

    public static LocalDateTime nowUtc() {
        return LocalDateTime.now(ZoneOffset.UTC);
    }

    /**
     * 当前时间的时间戳 时区采用 0 时区 使用时注意转换时区
     */
    public static Long currentTimeMillis() {
        return timeMillis(LocalDateTime.now(ZoneOffset.UTC));
    }

    public static Long timeMillis(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
    }

    public static String formatter() {
        return formatter(LocalDateTime.now());
    }

    public static String formatter(LocalDateTime localDateTime) {
        return formatter(localDateTime, HYPHEN_SPACE_COLON_SPACE_YYYY_MM_DD_HH_MM_SS_SSS);
    }

    public static String formatter(LocalDateTime localDateTime, String dateTimeFormatter) {
        return formatter(localDateTime, DateTimeFormatter.ofPattern(dateTimeFormatter));
    }

    public static String formatter(String dateTimeFormatter) {
        return formatter(LocalDateTime.now(), DateTimeFormatter.ofPattern(dateTimeFormatter));
    }

    public static String formatter(DateTimeFormatter dateTimeFormatter) {
        return formatter(LocalDateTime.now(), dateTimeFormatter);
    }

    public static String formatter(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter) {
        return dateTimeFormatter.format(localDateTime);
    }

    public static LocalDateTime parse(String datetime) {
        return parse(datetime, HYPHEN_SPACE_COLON_SPACE_YYYY_MM_DD_HH_MM_SS_SSS);
    }

    public static LocalDateTime parse(String datetime, String formatter) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatter);
        return parse(datetime, dateTimeFormatter);
    }

    public static LocalDateTime parse(String datetime, DateTimeFormatter dateTimeFormatter) {
        return LocalDateTime.parse(datetime, dateTimeFormatter);
    }
}
